$cpi, started, timer = [], false, Time.now

DownstreamHook.add('cp_ds', Proc.new {|ss|
  if ss.clean_colors =~ /have to kill \d \* (.+) \((.+?)(?: - Dead)?\)/
    mob_name, mob_regex, location, cpi_length, started = $1, /#{Regexp.quote($1)}$/i, $2, $cpi.length, true
    area = Map.areas.find {|k,v| v.name == location}[0]
    if area.nil?
      Map.rooms.each {|r| (area = r.zone;break) if r.name == location && Area.mobs(r.zone).find {|m| m[0] =~ mob_regex}}
      unless area.nil?
        name_rooms, mob_rooms = Map.find_room(area).inject([[], nil]) {|s,r|
          if r.name == location
            s[0] << r.num
            s[1] = (s[1].nil? ? [] : s[1]) + [r.num] if r.mobs.find {|m| m =~ mob_regex}
          end
          s
        }
        $cpi.insert($cpi.index {|cpi| cpi['a'] == area || !cpi.key?('a')} || $cpi.size, {'ss' => ss, 'a' => area, 'm' => mob_name, 'r' => mob_rooms || name_rooms})
      end
    else
      $cpi << {'ss' => ss, 'a' => area, 'm' => mob_name, 'r' => Area.mobs(area).find {|m| m[0] =~ mob_regex}[1]}
    end
    $cpi << {'ss' => ss} if cpi_length == $cpi.length
  elsif ss.clean_colors =~ /target names in this gquest|not on a global quest/
    DownstreamHook.remove('cp_ds')
    if started
      $app.add_boxes['chat'].display_text("="*80 + "\n")
      $cpi.inject(1) {|c, v|
        disp = v['ss'].gsub(/\*/, "* [#{cp_hunt_string(c)}]").strip
        disp += " -- #{go2_string(v['a'])} (#{go2_string(*v['r'])})\n"
        $app.add_boxes['chat'].display_text(disp)
        c + 1
      }
      $app.add_boxes['chat'].display_text("="*80 + "\n")
      echo Time.now - timer
    end
  end
  started ? nil : ss
})

$app.send_text('gquest check')